/*===========================================================================================================================
	Wolf Prop Protection
		By Goluch
			DO NOT UPLOAD TO GARRYSMOD.ORG
			
			
	Section: Action Tables
===========================================================================================================================*/
WPP.Actions = { };
WPP.Descriptions = { };
WPP.Tools = { };
WPP.Weapons = { };
WPP.WorldAcess = { };

/*===========================================================================================================================
	Name: WPP.RegisterAction
	Purpose: Creates an Action Class.
	Author: Goluch
===========================================================================================================================*/
function WPP:RegisterAction(ID, Name, Desc)
	local Index = #self.Actions + 1;
	_G["WPP_" .. string.upper(ID)] = Index
	self.Actions[ Index ] = Name;
	self.Descriptions[ Index ] = Desc || "N/A";
	print("Registed Action Class: (" ..Index .. ") " .. Name .. " -> " .. Desc);
	return Index;
end

/*===========================================================================================================================
	Name: WPP.LinkTools
	Purpose: Links Tools to an Action Class.
	Author: Goluch
===========================================================================================================================*/
function WPP:LinkTools(Index, ...)
	self.Tools[ Index ] = { ... };
end

/*===========================================================================================================================
	Name: WPP.GetFromTool
	Purpose: Gets an Action Class from a ToolMode.
	Author: Goluch
===========================================================================================================================*/
function WPP:GetFromTool(Tool)
	for Index,Value in pairs(self.Tools) do
		if ( table.HasValue(Value,Tool) ) then
			return Index;
		end
		
	end
	
	return WPP_TOOL
end

/*===========================================================================================================================
	Name: WPP.LinkWeapons
	Purpose: Links a weapon to an Action Class.
	Author: Goluch
===========================================================================================================================*/
function WPP:LinkWeapons(Index, ...)
	self.Weapons[ Index ] = { ... };
end

/*===========================================================================================================================
	Name: WPP.GetFromWeapon
	Purpose: Gets an Action Class from a weapon class.
	Author: Goluch
===========================================================================================================================*/
function WPP:GetFromWeapon(Weapon,Player)
	for Index,Value in pairs(self.Weapons) do
		if ( table.HasValue(Value,Weapon) ) then
			return Index;
		end
		
	end
	
	if ( Player ) and ( Weapon == "gmod_tool" ) then
		return self:GetFromTool(Player:GetInfo("gmod_toolmode"));
	end
	
end

/*===========================================================================================================================
	Name: WPP.SetWorldAcess
	Purpose: Sets the world access of an Action Class.
	Author: Goluch
===========================================================================================================================*/
function WPP:SetWorldAcess(Index, Bool)
	self.WorldAcess[ Index ] = Bool;
end

/*===========================================================================================================================
	Name: WPP.GetWorldAcess
	Purpose: Gets the world access of an Action Class.
	Author: Goluch
===========================================================================================================================*/
function WPP:GetWorldAcess(Index,Player)
	local Bool = self.WorldAcess[ Index ]
	if ( Bool != nil ) then
		if ( string.lower( type(Bool) ) == "function" ) then
			return Bool(Player);
		end
		
		return Bool;
	end
	
	return false;
end

/*===========================================================================================================================
	Name: WPP.IsValidAction
	Purpose: Confirms an Action Class is valid.
	Author: Goluch
===========================================================================================================================*/
function WPP:IsValidAction(Index)
	if ( self.Actions[ Index ] ) then
		return true;
	end
	
	return false;
end

/*===========================================================================================================================
	Name: WPP.ValidateAction
	Purpose: Checks if an action is possible.
	Author: Goluch
===========================================================================================================================*/
function WPP:ValidateAction(Index,Entity,Player)
	if ( !self:IsOwned(Entity) ) then 
		return self:GetWorldAcess(Index,Player);
	end
	
	if ( self:IsOwner(Entity,Player) ) then
		return true;
	end
	
	local Owner = self:GetOwner(Entity)
	if ( !Owner ) and ( WPP:GetOwnerSteam(Entity) ) and ( Player:IsSuperAdmin() ) then
		return true;
	elseif ( !Owner ) then
		return false;
	end
	
	if ( self:GetTrust(Owner,Player,Index) ) then
		return true;
	end
	
	if ( self:GetTrustGlobal(Owner,Index) ) then
		return true;
	end
	
	if ( WPP:GetShare(Entity,Index) ) then
		return true;
	end
	
	local Val = hook.Call("WPPAdminCheck",GAMEMODE,Index,Player,Owner);
	if ( Val != nil) then
		return Val;
	end //This can be used to change the way WPP uses admin rights.
	
	return false;
end


/*===========================================================================================================================
	Name: Gamemode.WPPAdminCheck
	Purpose: Checks if an admin action is possible.
	Author: Goluch
===========================================================================================================================*/
hook.Add("Initialize","WPP-AdminCheck-Function",function()
	if ( !GAMEMODE.WPPAdminCheck ) then //Allows this check to be built into a gamemode!
		function GAMEMODE:WPPAdminCheck(Index,Player,Owner)
			local Int = GetConVarNumber("wpp_allow_admins");
			if ( Player:IsAdmin() ) and ( Int >= 1 ) and ( Owner ) then
				if ( Player:IsSuperAdmin() ) then
					return true;
				elseif ( Owner:IsSuperAdmin() ) then
					return false;
				elseif ( Int == 1 ) then
					return true;
				end
				
				return false;
			end
			
		end
		
	end

end)